package wallegame.gui;

import static wallegame.Constants.TITLE_GAME;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

public class MyApplet extends JApplet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private NavigationPanel navigationPanel;
	private InstructionPanel instructionsPanel;
	private InfoPanel infoPanel;
	private GUIController guiController;
	private JPanel mainPanel;
	private boolean end = false;
	private JFrame container;
	private static final Dimension PREF_SIZE = new Dimension(900, 700);
	private MainWindow mainWindow = new MainWindow(guiController);

	public MyApplet() {
		super();
	}

	public void init() {
		try {
			javax.swing.SwingUtilities.invokeAndWait(new Runnable() {

				public void run() {
					container = new JFrame();
					mainWindow.setPreferredSize(PREF_SIZE);
					container.setTitle(TITLE_GAME);
					container.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

					mainPanel = new JPanel(new BorderLayout());

					mainWindow.createMenu();

					RobotPanel robotPanel = new RobotPanel();
					navigationPanel = new NavigationPanel();
					infoPanel = new InfoPanel();
					instructionsPanel = new InstructionPanel(guiController,
							robotPanel, mainWindow);
					JSplitPane SuperiorPanel = new JSplitPane(
							JSplitPane.HORIZONTAL_SPLIT, instructionsPanel,
							robotPanel);

					mainPanel.add(SuperiorPanel, BorderLayout.NORTH);

					JSplitPane inferiorPanel = new JSplitPane(
							JSplitPane.VERTICAL_SPLIT, SuperiorPanel,
							navigationPanel);
					mainPanel.add(inferiorPanel, BorderLayout.CENTER);
					mainPanel.add(infoPanel, BorderLayout.SOUTH);

					container.add(mainPanel);

					container.pack();
					container.setLocationRelativeTo(null);
					container.setVisible(true);

					mainWindow.add(container);

					guiController.registerRobotObserver(infoPanel);
					guiController.registerRobotObserver(mainWindow);
					guiController.registerRobotObserver(robotPanel);
					guiController.registerInventoryObserver(infoPanel);
					guiController.registerInventoryObserver(robotPanel);
					guiController.registerNavObserver(infoPanel);
					guiController.registerNavObserver(navigationPanel);
				}
			});
		} catch (Exception e) {
			System.err.println("createGUI didn't successfully complete");
		}
	}

	private void createGUI() {
		getContentPane().add(mainPanel);
	}

}